package com.zhkj.txg.module.mine.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.mine.entity.VisitLogResponse
import com.zhkj.txg.module.mine.model.MineManager

class VisitLogViewModel(application: Application) : BaseViewModel(application) {
    private val manager = MineManager()

    private val numb = 20
    var page = 1
        private set

    val visitLogLiveData = MutableLiveData<HttpResponse<VisitLogResponse>>()
    val deleteLogLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    fun getVisitLogList(restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<VisitLogResponse>(visitLogLiveData) {
            override fun onSuccess(resp: VisitLogResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val visitLog = manager.visitLog(AccountManager.getToken(), page, numb, listener)
        dispose.add(visitLog)
    }

    fun deleteVisitLog(ids: String) {
        if (ids.isEmpty()) return
        val listener = ViewModelSubscribeListener(deleteLogLiveData)
        val deleteVisitLog = manager.deleteVisitLog(AccountManager.getToken(), ids, listener)
        dispose.add(deleteVisitLog)
    }
}